package com.demo;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * <p>
 * <code>Person</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-06-06 14:26
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class Person0 {
    private Integer i = 0;

    public static void main(String[] args) {
        final Person0 person = new Person0();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    person.i++;
                    System.out.println(person.i);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    person.i++;
                    System.out.println(person.i);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}
/*
如果线程安全的，打印的i就不会重复
如果能直接操作内存里的数据，并发就不会有问题了
*/